home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / expect / exp_tty.c < prev    next >
C/C++ Source or Header  |  1993-03-15  |  5KB  |  223 lines

  1. /* exp_tty.c - tty support routines */
  2.  
  3. #include <stdio.h>
  4. #include "exp_conf.h"
  5. #include "tcl.h"
  6. #include "exp_global.h"
  7. #include "exp_rename.h"
  8. #include "exp_tty.h"
  9. #include "exp_log.h"
  10. #include "exp_command.h"
  11. #include "exp_main.h"
  12.  
  13. static int is_raw = FALSE;
  14. static int is_noecho = FALSE;
  15.  
  16. extern int dev_tty;
  17.  
  18. exp_tty tty_current, tty_cooked;
  19. int ioctled_devtty = FALSE;
  20.  
  21. /* if set == 1, set it to raw, else unset it */
  22. void
  23. tty_raw(set)
  24. int set;
  25. {
  26.     if (set == 1) {
  27.         is_raw = TRUE;
  28. #if defined(HAVE_TERMIOS) || defined(HAVE_TERMIO) /* had POSIX too */
  29.         tty_current.c_iflag = 0;
  30.         tty_current.c_oflag = 0;
  31.         tty_current.c_lflag &= ECHO;     /* disable everything except echo */
  32.         tty_current.c_cc[VMIN] = 1;
  33.         tty_current.c_cc[VTIME] = 0;
  34.     } else {
  35.         tty_current.c_iflag = tty_cooked.c_iflag;
  36.         tty_current.c_oflag = tty_cooked.c_oflag;
  37.         tty_current.c_lflag = tty_cooked.c_lflag;
  38.         tty_current.c_cc[VMIN] = tty_cooked.c_cc[VMIN];
  39.         tty_current.c_cc[VTIME] = tty_cooked.c_cc[VTIME];
  40. #else
  41. #  if defined(HAVE_SGTTYB)
  42.         tty_current.sg_flags |= RAW;
  43.     } else {
  44.         tty_current.sg_flags = tty_cooked.sg_flags;
  45. #  endif
  46. #endif
  47.         is_raw = FALSE;
  48.     }
  49. }
  50.     
  51. void
  52. tty_echo(set)
  53. int set;
  54. {
  55.     if (set == 1) {
  56.         is_noecho = FALSE;
  57. #if defined(HAVE_TERMIOS) || defined(HAVE_TERMIO) /* had POSIX too */
  58.         tty_current.c_lflag |= ECHO;
  59.     } else {
  60.         tty_current.c_lflag &= ~ECHO;
  61. #else
  62.         tty_current.sg_flags |= ECHO;
  63.     } else {
  64.         tty_current.sg_flags &= ~ECHO;
  65. #endif
  66.         is_noecho = TRUE;
  67.     }
  68. }
  69.  
  70. /* returns 0 if nothing changed */
  71. /* if something changed, the out parameters are changed as well */
  72. int
  73. tty_raw_noecho(interp,tty_old,was_raw,was_echo)
  74. Tcl_Interp *interp;
  75. exp_tty *tty_old;
  76. int *was_raw, *was_echo;
  77. {
  78.     if (exp_disconnected) return(0);
  79.     if (is_raw && is_noecho) return(0);
  80.     if (dev_tty == -1) return(0);
  81.  
  82.     *tty_old = tty_current;        /* save old parameters */
  83.     *was_raw = is_raw;
  84.     *was_echo = !is_noecho;
  85.     debuglog("tty_raw_noecho: was raw = %d  echo = %d\r\n",is_raw,!is_noecho);
  86.  
  87.     tty_raw(1);
  88.     tty_echo(-1);
  89.  
  90. #ifdef POSIX
  91.      if (tcsetattr(dev_tty, TCSADRAIN, &tty_current) == -1) {
  92. #else
  93.         if (ioctl(dev_tty, TCSETSW, &tty_current) == -1) {
  94. /***
  95.     if (ioctl(dev_tty, TIOCSETP, &tty_current) == -1) {
  96. ***/
  97. #endif
  98.         errorlog("ioctl(raw): %s\r\n",sys_errlist[errno]);
  99.         exp_exit(interp,-1);
  100.     }
  101.  
  102.     ioctled_devtty = TRUE;
  103.     return(1);
  104. }
  105.  
  106. /* returns 0 if nothing changed */
  107. /* if something changed, the out parameters are changed as well */
  108. int
  109. tty_cooked_echo(interp,tty_old,was_raw,was_echo)
  110. Tcl_Interp *interp;
  111. exp_tty *tty_old;
  112. int *was_raw, *was_echo;
  113. {
  114.     if (exp_disconnected) return(0);
  115.     if (!is_raw && !is_noecho) return(0);
  116.     if (dev_tty == -1) return(0);
  117.  
  118.     *tty_old = tty_current;        /* save old parameters */
  119.     *was_raw = is_raw;
  120.     *was_echo = !is_noecho;
  121.     debuglog("tty_cooked_echo: was raw = %d  echo = %d\r\n",is_raw,!is_noecho);
  122.  
  123.     tty_raw(-1);
  124.     tty_echo(1);
  125.  
  126. #ifdef POSIX
  127.      if (tcsetattr(dev_tty, TCSADRAIN, &tty_current) == -1) {
  128. #else
  129.         if (ioctl(dev_tty, TCSETSW, &tty_current) == -1) {
  130. /***
  131.     if (ioctl(dev_tty, TIOCSETP, &tty_current) == -1) {
  132. ***/
  133. #endif
  134.         errorlog("ioctl(noraw): %s\r\n",sys_errlist[errno]);
  135.         exp_exit(interp,-1);
  136.     }
  137.     ioctled_devtty = TRUE;
  138.  
  139.     return(1);
  140. }
  141.  
  142. void
  143. tty_set(interp,tty,raw,echo)
  144. Tcl_Interp *interp;
  145. exp_tty *tty;
  146. int raw;
  147. int echo;
  148. {
  149. #ifdef POSIX
  150.      if (tcsetattr(dev_tty, TCSADRAIN, tty) == -1) {
  151. #else
  152.         if (ioctl(dev_tty, TCSETSW, tty) == -1) {
  153. /***
  154.     if (ioctl(dev_tty, TIOCSETP, tty) == -1) {
  155. ***/
  156. #endif
  157.         errorlog("ioctl(set): %s\r\n",sys_errlist[errno]);
  158.         exp_exit(interp,-1);
  159.     }
  160.     is_raw = raw;
  161.     is_noecho = !echo;
  162.     tty_current = *tty;
  163.     debuglog("tty_set: raw = %d, echo = %d\r\n",is_raw,!is_noecho);
  164.     ioctled_devtty = TRUE;
  165. }    
  166.  
  167. void
  168. exp_init_tty()
  169. {
  170.     extern exp_tty exp_tty_original;
  171.  
  172.     /* save original user tty-setting in 'cooked', just in case user */
  173.     /* asks for it without earlier telling us what cooked means to them */
  174.     tty_cooked = exp_tty_original;
  175.  
  176.     /* save our current idea of the terminal settings */
  177.     tty_current = exp_tty_original;
  178.  
  179.     setbuf(stdout,(char *)0);    /* unbuffer stdout */
  180. }
  181.  
  182. /* take strings with newlines and insert carriage-returns.  This allows user */
  183. /* to write send_user strings without always putting in \r. */
  184. /* If len == 0, use strlen to compute it */
  185. /* NB: if terminal is not in raw mode, nothing is done. */
  186. char *
  187. exp_cook(s,len)
  188. char *s;
  189. int *len;    /* current and new length of s */
  190. {
  191.     static int destlen = 0;
  192.     static char *dest = 0;
  193.     char *d;        /* ptr into dest */
  194.     unsigned int need;
  195.  
  196.     if (s == 0) return("<null>");
  197.  
  198.     if (!is_raw) return(s);
  199.  
  200.     /* worst case is every character takes 2 to represent */
  201.     need = 1 + 2*(len?*len:strlen(s));
  202.     if (need > destlen) {
  203.         if (dest) free(dest);
  204.         if (!(dest = malloc(need))) {
  205.             destlen = 0;
  206.             return("malloc failed in cook");
  207.         }
  208.         destlen = need;
  209.     }
  210.  
  211.     for (d = dest;*s;s++) {
  212.         if (*s == '\n') {
  213.             *d++ = '\r';
  214.             *d++ = '\n';
  215.         } else {
  216.             *d++ = *s;
  217.         }
  218.     }
  219.     *d = '\0';
  220.     if (len) *len = d-dest;
  221.     return(dest);
  222. }
  223.